package littlepicture.welcome
{
	import flash.display.GradientType;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.geom.Matrix;
	import flash.utils.Timer;
	
	import mx.events.FlexEvent;
	import mx.preloaders.DownloadProgressBar;

	public final class PreLoader extends DownloadProgressBar
	{
		public function PreLoader(){
			super();
			
			var ws:WelcomeScreen = new WelcomeScreen();
			addChild(ws);
		}
		
		override public function set preloader(preloader:Sprite):void {
            // Listen for the relevant events
            preloader.addEventListener(FlexEvent.INIT_COMPLETE, myHandleInitEnd);
            stage.addEventListener(Event.RESIZE, resize);
            resize(null);
        }
        
        private function resize(event:Event):void{
			graphics.clear();
			var matr:Matrix = new Matrix();
  			matr.createGradientBox(stage.stageWidth,stage.stageHeight, Math.PI/2, 0, 0);
			graphics.beginGradientFill(GradientType.LINEAR,[0x000000, 0xDCD5D5],[0.4, 0.79],[0, 255],matr);
			graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight);
			graphics.endFill();
        }
		
		private function myHandleInitEnd(event:Event):void{
			var timer:Timer = new Timer(2500,1);
            timer.addEventListener(TimerEvent.TIMER, dispatchComplete);
            timer.start();
		}
		
		private function dispatchComplete(event:TimerEvent):void {
            dispatchEvent(new Event(Event.COMPLETE));
        }
	}
}